﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MyOA.Model;
using MyOA.DAL;
using System.Collections.Generic;
using MyOA.BLL;

namespace MyOA.UI.PersonManage.UserManage
{
    
    public partial class CreateOrUpdateUserInfo : System.Web.UI.Page
    {
        DepartInfoManager departInfomanager = new DepartInfoManager();
        UserInfoManager userInfoManager = new UserInfoManager();
        RoleInfoManager roleInfoManager = new RoleInfoManager();
        UserStateManager userStateManager = new UserStateManager();
        BranchInfoService branchInfoService = new BranchInfoService();
        string message = string.Empty;  //提示信息
  
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //未登录先登录
                if (Session["loginId"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                }
                //登录加载
                else
                {
                    string userId = Request.QueryString["userId"];
                    // LoadDepartInfo();           //绑定部门下拉列表的值
                    LoadBranchInfo();            //绑定机构下拉列表的值
                    LoadUserStateInfo();        //绑定状态下拉列表的信息
                    LoadRoleInfo();             //绑定角色下拉列表的值
                    if (userId != null)
                    {
                        //如果点击修改 侧显示信息
                        DisplayDepartInfo(userId);
                    }
                }
            }
           
          
        }
        //机构选择事件
        protected void ddlBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            ddlDepart.Items.Clear();          //清空下拉列表
            int branchId = Convert.ToInt32(ddlBranch.SelectedValue);
            LoadDepartInfoByBranchId(branchId);
            ddlDepart.Enabled = true;
           
        }
        //绑定机构信息
        private void LoadBranchInfo()
        {
            IList<BranchInfo> branchList = branchInfoService.GetAllBranchInfo();
            foreach (BranchInfo branchInfo in branchList)
            {
                ListItem li = new ListItem();
                li.Value = branchInfo.BranchId.ToString();
                li.Text = branchInfo.BranchName;
                ddlBranch.Items.Add(li);
            }
            ddlBranch.Items.Add(new ListItem("==请选择==","0"));
            ddlBranch.SelectedValue = "0";
        }
        //根据BranchId绑定部门下拉列表的信息
        public void LoadDepartInfoByBranchId(int branchId)
        {
            IList<DepartInfo> departInfoList = departInfomanager.getAllDepartInfoByBranchId(branchId);
            foreach (DepartInfo departInfo in departInfoList)
            {
                ListItem li = new ListItem();
                li.Value = departInfo.DepartId.ToString();
                li.Text = departInfo.DepartName;
                ddlDepart.Items.Add(li);
            }
        }
        //绑定部门下拉列表的信息
        public void LoadDepartInfo()
        {
            IList<DepartInfo> departInfoList = departInfomanager.GetAllDepartInfo();
            foreach (DepartInfo departInfo in departInfoList)
            {
                ListItem li = new ListItem();
                li.Value = departInfo.DepartId.ToString();
                li.Text = departInfo.DepartName;
                ddlDepart.Items.Add(li);
            }
        }
        //绑定状态下拉列表的信息
        public void LoadUserStateInfo()
        {
            IList<UserState> userStateList = userStateManager.GetAllUserState();
            foreach (UserState userState in userStateList)
            {
                ListItem li = new ListItem();
                li.Value = userState.UserStateId.ToString();
                li.Text = userState.UserStateName;
                ddlUserState.Items.Add(li);
            }
        }
        //绑定角色下拉列表的值
        public void LoadRoleInfo()
        {
            IList<RoleInfo> roleInfoList = roleInfoManager.GetAllRoleInfo();
            foreach (RoleInfo roleInfo in roleInfoList)
            {
                ListItem li = new ListItem();
                li.Value = roleInfo.RoleId.ToString();
                li.Text = roleInfo.RoleName;
                ddlRole.Items.Add(li);
            }
        }
        //如果点击修改 侧显示信息
        public void DisplayDepartInfo(string userId)
        {
            int Id = Convert.ToInt32(userId);
            UserInfo userInfo = userInfoManager.GetUserInfoByUserId(Id);
            this.txtLoginId.Text = userInfo.LoginId.ToString();
            this.txtPassword.Text = userInfo.Password.ToString();
            this.txtSureword.Text = userInfo.Password.ToString();
            this.txtTrueName.Text = userInfo.UserName;
            this.ddlRole.SelectedValue = userInfo.Role.RoleId.ToString();
            this.ddlUserState.SelectedItem.Text = userInfo.UserState.UserStateName;
            this.ddlDepart.SelectedValue = userInfo.Depart.DepartId.ToString();
            this.rdolstGender.SelectedValue = userInfo.Gender.ToString();
        }
        //添加UserInfo信息
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            UserInfo userInfo = new UserInfo();
            userInfo.LoginId = this.txtLoginId.Text;
            userInfo.Password = this.txtPassword.Text;
            userInfo.UserName = this.txtTrueName.Text;
            int departId = Convert.ToInt32(ddlDepart.SelectedItem.Value);
            userInfo.Depart = departInfomanager.GetDepartInfoBuDepartInfoByDepartId(departId);
            userInfo.Gender = Convert.ToInt32(rdolstGender.SelectedItem.Value);
            int roleId = Convert.ToInt32(ddlRole.SelectedItem.Value);
            userInfo.Role = roleInfoManager.GetRoleInfoByRoleId(roleId);
            int userStateId = Convert.ToInt32(ddlUserState.SelectedItem.Value);
            userInfo.UserState = userStateManager.getUserStateByUserStateId(userStateId);
            if (Request.QueryString["userId"] != null)
            {
                int userId = Convert.ToInt32(Request.QueryString["userId"]);
                userInfo.UserId = userId;
                int result = userInfoManager.UpdateUserInfoByUserId(userInfo);
                if (result > 0)
                {
                    message = "修改成功";
                }
                else
                {
                    message = "修改失败";
                }
            }
            else
            {
                if (userInfoManager.GetUserInfoByLoginId(userInfo.LoginId).LoginId == "")
                {
                    int result = userInfoManager.AddUserInfo(userInfo);
                    if (result > 0)
                    {
                        message = "添加成功";
                    }
                    else
                    {
                        message = "添加失败";
                    }
                }
                else
                {
                    message = "对不起，该用户已存在！";
                }
            }
            string script = string.Format("<script>Alert('{0}',null,null,'温馨提示','href()')</script>", message);
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), " ", script, false);

        }
        //返回按钮
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserManage.aspx");
        }
        //重新填写

        protected void Button1_Click1(object sender, EventArgs e)
        {
            this.txtLoginId.Text = " ";
            this.txtPassword.Text = " ";
            this.txtSureword.Text = " ";
            this.txtTrueName.Text = " ";
            this.ddlBranch.SelectedValue = "0";
            this.ddlDepart.Items.Clear();//清空部门
           
        }

        protected void txtPassword_TextChanged(object sender, EventArgs e)
        {
          
        }

    }
}
